import CryptoJS from 'crypto-js/crypto-js'

//加密
export const Encrypt = (word, keyStr, ivStr) => {
    let key = CryptoJS.enc.Utf8.parse('0dc17cdd5cd2fe14')
    let iv = CryptoJS.enc.Utf8.parse('1234567890123456')
    let srcs = CryptoJS.enc.Utf8.parse(word)
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Iso10126
    })
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}

/**
 * AES 解密 ：字符串 key iv 返回base64
 *
 * @return {string}
 */
export const Decrypt = (word) => {
    // console.log(word === word.replace(/\s+/g, ''))

    let key = CryptoJS.enc.Utf8.parse('0dc17cdd5cd2fe14')
    let iv = CryptoJS.enc.Utf8.parse('1234567890123456')

    let decrypt = CryptoJS.AES.decrypt(word, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Iso10126
    })

    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    // console.log(decryptedStr, 'decryptedStr')
    return decryptedStr.toString().trim()
}